package cn.bgs.controller;

import cn.bgs.pojo.ExamStorage;
import cn.bgs.pojo.Student;
import cn.bgs.service.ExamService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author Mr.JIA
 * @DATE 2021/8/4 0004 8:38
 * @PROJECT_NAME mybatis_phoenix
 * @PACKAGE_NAME cn.bgs.controller
 */
@Controller
@RequestMapping("/exam")
public class ExamController {


    Logger log = LogManager.getLogger();
    @Autowired
    private ExamService examService;

    //添加 exam
    @RequestMapping("/addExam")
    @ResponseBody
    public Integer addExam(@RequestBody String ex){
        JSONObject jsonObject = JSONObject.parseObject(ex);
        ExamStorage ee = new ExamStorage();
        ee.setGoodsName(jsonObject.getString("goodsName"));
        ee.setGoodsAmount(jsonObject.getInteger("goodsAmount"));
        ee.setGoodsType(jsonObject.getString("goodsType"));
        ee.setRemark(jsonObject.getString("remark"));
        Calendar now = Calendar.getInstance();
        ee.setCreateTime(now.getTime());
        ee.setUpdateTime(now.getTime());
        return examService.addExam(ee);
    }

    //删除 exam
    @RequestMapping("/delExam")
    @ResponseBody
    public Integer delExam(@RequestBody String del){
        JSONObject jsonObject = JSONObject.parseObject(del);
        Integer pkId = jsonObject.getInteger("pkId");

        return examService.delExam(pkId);
    }


    //查询 exam
    @RequestMapping("/findAllExam")
    @ResponseBody
    public Map<String,Object> findAllExam(@RequestParam(required=false,defaultValue="1") int page,
                                         @RequestParam(required=false,defaultValue="7") int limit,
                                          ExamStorage ex){
        // 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
        // 从他的下一条sql开始分页
        PageHelper.startPage(page, limit);
//        List<Student> stu  = stuService.findAllStu(stuName,mobile,sex,age,idNum);// 这是我们的sql
//        for(Student i:stu){
//            i.setNativePlace(i.getNativePlace());
//        }

        log.info("String goodsName, Integer goodsAmount, String goodsType：：：：：：："
                +ex.getGoodsName()+"   "
                +ex.getGoodsAmount()+"   "
                +ex.getGoodsType()+"   "

        );
        List<Student> exam = examService.findAllExam(ex);
        log.info("data======::::::::"+exam);
        // 使用pageInfo包装查询
        PageInfo<Student> rolePageInfo = new PageInfo<>(exam);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("count",rolePageInfo.getTotal());
        map.put("data",rolePageInfo.getList());
        log.info("map------------------------------------"+map);
        return map;
    }
}
